package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;


public class RiskZoneAll {

	RiskZone r;
	String order;
	ListView listView;
	ArrayList<RiskZone> riskFromDB;
	ArrayList<RiskZone> allRiskZone = new ArrayList<RiskZone>();
	InputStream is = null;
	String js_result = "";
	HttpPost httppost = null;
	MemberUser user = new MemberUser();
	static UrlEmerMez URLlink = new UrlEmerMez();
	public static final String urlShowRiskZoneEachUserEnable = URLlink.getUrlshowriskzoneeachuserenable();
	public static final String urlShowRiskZoneAllBy = URLlink.getUrlshowriskzoneallby();
	public static final String urlShowRiskZoneMyBy = URLlink.getUrlshowriskzonemyby();
	//public static final String urlUpdateRiskZoneEnable = UrlEmerMez.URLUpdateRiskZoneEnable;
	public static String result = "";
	WebPageTask task; 
	String userId,riskId;
	String type;
	String enable;
	String orderBy;
	public void callRun() {
		task = new WebPageTask();
		task.execute();
		//
		
	}
	
	public void setVariable(String id,String enable){
		userId= id;
		this.enable = enable;
	}
	public ArrayList<RiskZone> getRiskFromDB() {
		return riskFromDB;
	}

	public void setRiskFromDB(ArrayList<RiskZone> riskFromDB) {
		this.riskFromDB = riskFromDB;
	}

	public ArrayList<RiskZone> getAllRiskZone() {
		ArrayList<RiskZone> values = new ArrayList<RiskZone>();		
		Log.d("result",""+result.length());
		try {

			JSONArray jArray = new JSONArray(result);
			Log.d("ValueArray1",""+result.length());
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject jo = jArray.getJSONObject(i);

				String risk_id = jo.getString("risk_id");
				String user_id = jo.getString("user_id");
				String nameStart = jo.getString("nameStart");
				String nameEnd = jo.getString("nameEnd");
				String nickName = jo.getString("risk_nickname");
				String oriLat = jo.getString("oriLat");
				String oriLng = jo.getString("oriLng");
				String destLat = jo.getString("destLat");
				String destLng = jo.getString("destLng");
				String type = jo.getString("type");
				String ratingAvg = jo.getString("rating_avg");
				String ratingSum = jo.getString("rating_sum");
				String ratingCount = jo.getString("rating_count");				
				String date_riskZone = jo.getString("date_riskZone");
				String time_riskZone = jo.getString("time_riskZone");
				String enable = jo.getString("enable");
				
				r = new RiskZone();
				r.setId(risk_id);
				r.setUserId(user_id);
				r.setNameStart(nameStart);
				r.setNameEnd(nameEnd);
				r.setOiriginLat(oriLat);
				r.setOriginLng(oriLng);
				r.setDestinationLat(destLat);
				r.setDestinationLng(destLng);
				r.setType(type);
				r.setTime(time_riskZone);
				r.setDate(date_riskZone);
				r.setRatingAvg(ratingAvg);
				r.setRatingCount(ratingCount);
				r.setRatingSum(ratingSum);
				r.setEnable(enable);
				r.setNickName(nickName);
				values.add(r);
				
				Log.d("Enable",""+enable);
				Log.d("ValueArray",""+values.size());
				Log.d("NameStart",""+r.getNameStart());
				
			}
			Log.d("ValueArray",""+values.size());
		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
			Log.d("LoveLog","Error parsing data " + e.toString());

		}
		
		return values;
		
		
	}
	
	public String ShowRiskZone() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("userid", userId ));
		nameValuePairs.add(new BasicNameValuePair("enable", enable ));
		Log.d("Hello",""+userId);
		return postData(urlShowRiskZoneEachUserEnable, nameValuePairs);
	}

	public String postData(String urlShow,List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(urlShow);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");

		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			String line;
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			js_result = sb.toString();
			return js_result;
		} catch (ClientProtocolException e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		} catch (IOException e) {
		}
		return null;
	}

	/**
	 * @author Living_Room
	 *
	 */
	protected class WebPageTask extends AsyncTask<Void, Void, String> {
		
		@Override
		protected String doInBackground(Void... params) {
			result = ShowRiskZone();
			RiskZoneAll risk = new RiskZoneAll();
			
			riskFromDB = risk.getAllRiskZone();
			Log.d("Riskss",""+risk.getAllRiskZone().size());
			return result;
		}

		protected void onPostExecute(String result) {
			

		}
		

	}
	
	
	
	
	
	// Another Method
	// Available here
	public void setVariable1(String userId,String type,String orderBy){		
		this.userId = userId;
		this.type = type;
		this.orderBy= orderBy;
	}
	public void callRunShowRiskAllByTypeOrderBy(){
		ShowRiskByTypeAndRating task = new ShowRiskByTypeAndRating();
		task.execute();
	}
	public String setValuePairForShowRiskEachUserByTypeOrderBy() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);	
		nameValuePairs.add(new BasicNameValuePair("userId", userId ));
		nameValuePairs.add(new BasicNameValuePair("type", type ));
		nameValuePairs.add(new BasicNameValuePair("orderBy", orderBy ));
		Log.d("Hello",""+type);
		return postData2(urlShowRiskZoneAllBy, nameValuePairs);
	}

	public String postData2(String urlShow,List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(urlShow);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");

		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			String line;
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			js_result = sb.toString();
			return js_result;
		} catch (ClientProtocolException e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		} catch (IOException e) {
		}
		return null;
	}
	protected class ShowRiskByTypeAndRating extends AsyncTask<Void, Void, String> {
		
		@Override
		protected String doInBackground(Void... params) {
			result = setValuePairForShowRiskEachUserByTypeOrderBy();
			RiskZoneAll risk = new RiskZoneAll();
			
			riskFromDB = risk.getAllRiskZone();
			Log.d("Riskss",""+risk.getAllRiskZone().size());
			return result;
		}

		protected void onPostExecute(String result) {
			

		}	
	}
	
	
	// Another Method
	// Available here
	public void setVariableForListRiskZoneMyBy(String userId,String type, String orderBy){
		
		this.userId = userId;
		this.order = orderBy;
		this.type = type;
	}
	public void callRunShowRiskMyBy(){
		ShowRiskMyByType task = new ShowRiskMyByType();
		task.execute();
	}
	public String setValuePairForshowRiskMyByTypeAndRating() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("userid", userId ));
		nameValuePairs.add(new BasicNameValuePair("type", type ));
		nameValuePairs.add(new BasicNameValuePair("orderBy", order ));
		Log.d("Hello",""+type);
		return postData3(urlShowRiskZoneMyBy, nameValuePairs);
	}

	public String postData3(String urlShow,List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(urlShow);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");

		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			String line;
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			js_result = sb.toString();
			return js_result;
		} catch (ClientProtocolException e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		} catch (IOException e) {
		}
		return null;
	}
	protected class ShowRiskMyByType extends AsyncTask<Void, Void, String> {
		
		@Override
		protected String doInBackground(Void... params) {
			result = setValuePairForshowRiskMyByTypeAndRating();
			RiskZoneAll risk = new RiskZoneAll();
			
			riskFromDB = risk.getAllRiskZone();
			Log.d("Riskss",""+risk.getAllRiskZone().size());
			return result;
		}

		protected void onPostExecute(String result) {
			

		}
		
		
		
		
	}
	
	//Update RiskZone Enable
	//Method
			public void setVariableForUpdateRiskZoneEnable(String userId,String riskId,String enable){
				
				this.userId = userId;
				this.riskId = riskId;
				this.enable = enable;
			}
			public void callRunUpdateRiskZoneEnable(){
				UpdateRiskZoneEnable task = new UpdateRiskZoneEnable();
				task.execute();
			}
			public String setValuePairForUpdateRiskZoneEnable() {
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
				nameValuePairs.add(new BasicNameValuePair("userId", userId ));				
				nameValuePairs.add(new BasicNameValuePair("riskId", riskId ));
				nameValuePairs.add(new BasicNameValuePair("enable", enable ));
				Log.d("Hello",""+type);
				return null;
						// postData4(urlUpdateRiskZoneEnable, nameValuePairs);
			}

			public String postData4(String urlShow,List<NameValuePair> nameValuePairs) {

				HttpClient httpclient = new DefaultHttpClient();
				HttpPost httppost = new HttpPost(urlShow);
				BufferedReader in = null;
				StringBuffer sb = new StringBuffer("");

				try {
					httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
					HttpResponse response = httpclient.execute(httppost);
					in = new BufferedReader(new InputStreamReader(response.getEntity()
							.getContent()));
					String line;
					String NL = System.getProperty("line.separator");
					while ((line = in.readLine()) != null) {
						sb.append(line + NL);
					}
					in.close();
					js_result = sb.toString();
					return js_result;
				} catch (ClientProtocolException e) {
					Log.e("log_tag", "Error converting result " + e.toString());
				} catch (IOException e) {
				}
				return null;
			}
			protected class UpdateRiskZoneEnable extends AsyncTask<Void, Void, String> {
				
				@Override
				protected String doInBackground(Void... params) {
					result = setValuePairForUpdateRiskZoneEnable();					
					return result;
				}

				protected void onPostExecute(String result) {
					

				}
		
	
	
	
	
	
}
			}